const express = require('express')
// 自定义的中间件
// 将全局的路由全部都托管到 router 下面来进行处理 
const router = require('./router')
// 创建 express 应用
const app = express()
// 跨域扩展
const cors = require('cors')
// 控制台打印
const bodyParser = require('body-parser')

// https 服务
// const fs = require('fs')
// const https = require('https')

// const privateKey = fs.readFileSync('https/book_youbaobao_xyz.key', 'utf8')
// const pem = fs.readFileSync('https/book_youbaobao_xyz.pem', 'utf8')
// const credentials = {
//   key: privateKey,
//   cert: pem
// }
// const httpsServer = https.createServer(credentials, app)

// httpsServer.listen(18082, function () {
//   console.log('HTTPS Server is running on: https://localhost:%s', SSLPORT)
// })

app.use(cors())
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
app.use('/', router)

// 使 express 监听 5000 端口号发起的 http 请求
const server = app.listen(18082, function() {
  const { address, port } = server.address()
  console.log('Http Server is running on http://%s:%s', address, port)
})